﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using System.Configuration;

namespace SdmxMl.Multiplexor
{
    public class ProxyService
    {
        private RegProxy tinyProxy;
        private SoapRegProxy sdmxProxy;
        private string _url;

        public bool useSdmxProxy { get; private set; }

                /// <summary> NSI user ID in SMS system</summary>
        public string UserId 
        { 
            get { return useSdmxProxy ? sdmxProxy.UserId : tinyProxy.UserId; }
        }

        public string LastError
        {
            get { return useSdmxProxy ? sdmxProxy.LastError : tinyProxy.LastError; }
        }

        /// <summary>Current state of connexion</summary>
        public RepositoryState State
        {
            get { return useSdmxProxy ? sdmxProxy.State : tinyProxy.State; }
            set
            {
                if (useSdmxProxy)
                    sdmxProxy.State = value;
                else
                    tinyProxy.State = value;
            }
        }


        public bool UserIsAdmin
        {
            get { return useSdmxProxy ? sdmxProxy.UserIsAdmin : tinyProxy.UserIsAdmin; }
            set
            {
                if (useSdmxProxy)
                    sdmxProxy.UserIsAdmin = value;
                else
                    tinyProxy.UserIsAdmin = value;
            }
        }



        public string Url { get { return _url; } }

        // Version of Web service used
        public double Version
        {
            get { return useSdmxProxy ? sdmxProxy.Version : tinyProxy.Version; }
            set
            {
                if (useSdmxProxy)
                    sdmxProxy.Version = value;
                else
                    tinyProxy.Version = value;
            }
        }


        public bool BackupAutomatic
        {
            get { return useSdmxProxy ? sdmxProxy.BackupAutomatic : tinyProxy.BackupAutomatic; }
            set
            {
                if (useSdmxProxy)
                    sdmxProxy.BackupAutomatic = value;
                else
                    tinyProxy.BackupAutomatic = value;
            }
        }



        public ProxyService(string urn, string vs, string userId)
        {
            string artefactRegistryService = ConfigurationManager.AppSettings["artefactRegistryService"] != null ?
                ConfigurationManager.AppSettings["artefactRegistryService"].ToString() : null;

            useSdmxProxy = artefactRegistryService != null;
            _url = urn;

            // instantiate
            if (useSdmxProxy)
            {
                sdmxProxy = new SoapRegProxy(urn, vs, userId);
            }
            else
                tinyProxy = new RegProxy(urn, vs, userId);

        }

        public RepositoryState Test()
        {
            return (useSdmxProxy) ? sdmxProxy.Test() : tinyProxy.Test();
        }

        public string GetLastVersion(string arType, string fullIdent)
        {
            return (useSdmxProxy) ? sdmxProxy.GetLastVersion(arType, fullIdent) : tinyProxy.GetLastVersion(arType, fullIdent);
        }

        public XmlDocument GetArtefact(string arType, string fullIdent)
        {
            return (useSdmxProxy) ? sdmxProxy.GetArtefact(arType, fullIdent) : tinyProxy.GetArtefact(arType, fullIdent);
        }

        public bool ExistArtefact(string arType, string fullIdent)
        {
            return (useSdmxProxy) ? sdmxProxy.ExistArtefact(arType, fullIdent) : tinyProxy.ExistArtefact(arType, fullIdent);
        }

        public void DeleteArtefact(string arType, string fullIdent)
        {
            if (useSdmxProxy)
                sdmxProxy.DeleteArtefact(arType, fullIdent);
            else
                tinyProxy.DeleteArtefact(arType, fullIdent);
        }

        public void SetDisseminationStatus(string arType, string fullIdent, string diffusionCd)
        {
            if (useSdmxProxy)
                sdmxProxy.SetDisseminationStatus(arType, fullIdent, diffusionCd);
            else
                tinyProxy.SetDisseminationStatus(arType, fullIdent, diffusionCd);
        }

        public void SubmitArtefactBackup(string arType, string fullIdent, XmlElement element)
        {
            if (useSdmxProxy)
                sdmxProxy.SubmitArtefactBackup(arType, fullIdent, element);
            else
                tinyProxy.SubmitArtefactBackup(arType, fullIdent, element);
        }

        public void SubmitArtefact(string arType, string fullIdent, XmlElement element)
        {
            if (useSdmxProxy)
                sdmxProxy.SubmitArtefact(arType, fullIdent, element);
            else
                tinyProxy.SubmitArtefact(arType, fullIdent, element);
        }

        public void CheckOut(string arType, string fullIdent)
        {
            if (useSdmxProxy)
                sdmxProxy.CheckOut(arType, fullIdent);
            else
                tinyProxy.CheckOut(arType, fullIdent);
        }

        public void UndoCheckOut(string arType, string fullIdent)
        {
            if (useSdmxProxy)
                sdmxProxy.UndoCheckOut(arType, fullIdent);
            else
                tinyProxy.UndoCheckOut(arType, fullIdent);
        }

        public System.Data.DataSet DirInfo(string arType)
        {
            return (useSdmxProxy) ? sdmxProxy.DirInfo(arType) : tinyProxy.DirInfo(arType);
        }

        public System.Data.DataSet GetArtefactInfos(string artTypePattern, string agencyPattern, string idPattern, string versionPattern, DateTime start, DateTime end)
        {
            return (useSdmxProxy) ? sdmxProxy.GetArtefactInfos(artTypePattern, agencyPattern, idPattern, versionPattern, start, end) :
                tinyProxy.GetArtefactInfos(artTypePattern, agencyPattern, idPattern, versionPattern, start, end);
        }

        public bool IsBackupExist(string arType, string fullIdent)
        {
            return (useSdmxProxy) ? sdmxProxy.IsBackupExist(arType, fullIdent) : tinyProxy.IsBackupExist(arType, fullIdent);
        }

        public System.Data.DataSet GetBackupList(string arType, string fullIdent)
        {
            return (useSdmxProxy) ? sdmxProxy.GetBackupList(arType, fullIdent) : tinyProxy.GetBackupList(arType, fullIdent);
        }

        public XmlDocument GetArtefactBackup(string arType, string fullIdent, string versionNumber)
        {
            return (useSdmxProxy) ? sdmxProxy.GetArtefactBackup(arType, fullIdent, versionNumber) :
                tinyProxy.GetArtefactBackup(arType, fullIdent, versionNumber);
        }

        public bool IsAdministrator() { return IsAdministrator(UserId); }
        public bool IsAdministrator(string uid)
        {
            return (useSdmxProxy) ? sdmxProxy.IsAdministrator(uid) : tinyProxy.IsAdministrator(uid);
        }

        public bool IsOwner(string uid, string fullId)
        {
            return (useSdmxProxy) ? sdmxProxy.IsOwner(uid, fullId) : tinyProxy.IsOwner(uid, fullId);
        }

        public void UpdateFinalizedArtefact(string arType, string fullIdent, XmlDocument xDoc)
        {
            if (useSdmxProxy)
                sdmxProxy.UpdateFinalizedArtefact(arType, fullIdent, xDoc);
            else
                tinyProxy.UpdateFinalizedArtefact(arType, fullIdent, xDoc);
        }

        public void UpdateOwner(string arType, string fullIdent, string ownerId)
        {
            if (useSdmxProxy)
                sdmxProxy.UpdateOwner(arType, fullIdent, ownerId);
        }

        /// <summary>
        /// Obtain list of ownership group where user is associated
        /// </summary>
        /// <returns>the list of associated owner group</returns>
        public List<string> UserOwnerList()
        {
            List<string> list = new List<string>();
            if (useSdmxProxy)
                list = sdmxProxy.UserOwnerList(UserId, IsAdministrator());
            return list;
        }

        public Ch.Admin.Bfs.Sms.Sdmx.Registry.Rights.ISecuredArtefactRegistry GetNewRegistry()
        {
            return useSdmxProxy ? sdmxProxy.artefactRegistry : null;
        }

        public Ch.Admin.Bfs.Sms.Sdmx.Registry.Proxies.WcfAdministrationProxy GetAdminRegistry()
        {
            return useSdmxProxy ? sdmxProxy.administrationProxy : null;
        }
        public Ch.Admin.Bfs.Sms.Sdmx.Registry.Proxies.WcfNotificationsProxy GetNotificationRegistry()
        {
            return useSdmxProxy ? sdmxProxy.notificationProxy : null;
        }
    }
}
